package com.android.commonlib.loop;


import android.os.Handler;

/**
 * Created by zhuqin on 2018/3/8.
 */

public class HandlerLooper {

    private int delay = 30 * 1000;

    private Handler mHandler ;

    private TaskContent task ;

    public static HandlerLooper getInstante(){
        HandlerLooper mHandlerLooper = new HandlerLooper();
        mHandlerLooper.onCreate();
        return mHandlerLooper ;
    }

    private Runnable taskRunnable = new Runnable() {
        @Override
        public void run() {
            if(task != null) {
                task.excuteTask();
            }
            mHandler.postDelayed(taskRunnable,delay);
        }
    };

    public void onCreate(){
        mHandler = new Handler();
    }

    public void onStart(int delay ,TaskContent task){
        this.delay = delay ;
        this.task = task ;
        mHandler.removeCallbacks(taskRunnable);
        mHandler.postDelayed(taskRunnable , delay);
    }

    public void onDestory(){
        mHandler.removeCallbacks(taskRunnable);
    }


    public interface TaskContent{
        void excuteTask();
    }

}
